package com.mf.ergate.web.controller.opt;

import com.mf.ergate.api.opt.model.dto.*;
import com.mf.ergate.api.opt.model.vo.OrgContractVo;
import com.mf.ergate.api.opt.model.vo.OrgInfoVo;
import com.mf.ergate.api.sys.model.dto.DistrictInfoQueryDto;
import com.mf.ergate.api.sys.model.vo.DistrictInfoVo;
import com.mf.ergate.web.common.utils.MfParamUtils;
import com.mf.ergate.web.domain.enums.generic.IsEnabledEnum;
import com.mf.ergate.web.domain.enums.opt.ContractTypeEnum;
import com.mf.ergate.web.service.opt.OrgInfoService;
import com.mf.framework.common.model.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 机构信息Controller
 * User: zhaoming
 * DateTime: 2019-03-25
 * To change this template use File | Settings | File Templates.
 */
@Secured({"MANAGE_OPT_ORG_INFO"})
@Controller
@RequestMapping("/web/orgInfo")
public class OrgInfoController{

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private OrgInfoService orgInfoService;

    /**
     * 首页
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("opt/org/org_info_index");
        modelAndView.addObject("IsEnabledEnum", IsEnabledEnum.getList());
        return modelAndView;
    }

    /**
     * 新增页面
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage() {
        ModelAndView modelAndView = new ModelAndView("opt/org/org_info_add");
        return modelAndView;
    }

    /**
     * 维护页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/org/org_info_edit");
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 合同配置页面
     * @param uniqueNo 唯一编号
     * @return 模型视图
     */
    @RequestMapping(value = "/contractConfigPage", method = RequestMethod.GET)
    public ModelAndView contractConfigPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("opt/org/org_info_config_contract");
        modelAndView.addObject("orgInfo", orgInfoService.selectByUniqueNo(uniqueNo));
        modelAndView.addObject("ContractTypeEnum", ContractTypeEnum.getList());
        return modelAndView;
    }

    /**
     * 新增
     * @param orgInfoCreateDto 机构信息新增
     * @return 机构信息
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public OrgInfoVo insert(OrgInfoCreateDto orgInfoCreateDto) {
        MfParamUtils.buildInsertEntity(orgInfoCreateDto);
        return orgInfoService.insert(orgInfoCreateDto);
    }

    /**
     * 更新
     * @param orgInfoUpdateDto 机构信息更新
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean update(OrgInfoUpdateDto orgInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(orgInfoUpdateDto);
        return orgInfoService.updateByUniqueNo(orgInfoUpdateDto);
    }

    /**
     * 根据主键删除(逻辑删除)
     * @param orgInfoDeleteDto 机构信息删除
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(OrgInfoDeleteDto orgInfoDeleteDto){
        MfParamUtils.buildUpdateEntity(orgInfoDeleteDto);
        return orgInfoService.deleteByUniqueNo(orgInfoDeleteDto);
    }

    /**
     * 根据业务主键查询
     * @param uniqueNo 唯一编号
     * @return 机构信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public OrgInfoVo detail(String uniqueNo) {
        return orgInfoService.selectByUniqueNo(uniqueNo);
    }

    /**
     * 根据条件查询
     * @param orgInfoQueryDto 机构信息查询
     * @return 机构信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<OrgInfoVo> list(OrgInfoQueryDto orgInfoQueryDto) {
        return orgInfoService.selectByCondition(orgInfoQueryDto);
    }

    /**
     * 根据条件分页查询
     * @param orgInfoQueryDto 机构信息查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 机构信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<OrgInfoVo> page(OrgInfoQueryDto orgInfoQueryDto,
                                    @RequestParam(value = "page") int pageNum,
                                    @RequestParam(value = "rows") int pageSize,
                                    @RequestParam(value = "sidx") String sortName,
                                    @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return orgInfoService.selectPage(page, orgInfoQueryDto);
    }

    /**
     * 根据条件查询行政区列表
     * @param districtInfoQueryDto 行政区信息查询Dto
     * @return 行政区列表
     */
    @RequestMapping(value = "/listDistrictInfo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<DistrictInfoVo> listDistrictInfo(DistrictInfoQueryDto districtInfoQueryDto) {
        if(StringUtils.isBlank(districtInfoQueryDto.getParentDistrictCode())){
            districtInfoQueryDto.setParentDistrictCode("0");
        }
        return orgInfoService.listDistrictInfo(districtInfoQueryDto);
    }

    /**
     * 启用
     * @param orgInfoUpdateDto 机构信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/enableByUniqueNo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean enableByUniqueNo(OrgInfoUpdateDto orgInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(orgInfoUpdateDto);
        orgInfoUpdateDto.setIsEnable(IsEnabledEnum.YES.getIndex());
        return orgInfoService.updateByUniqueNo(orgInfoUpdateDto);
    }

    /**
     * 停用
     * @param orgInfoUpdateDto 机构信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/disableByUniqueNo", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean disableByUniqueNo(OrgInfoUpdateDto orgInfoUpdateDto) {
        MfParamUtils.buildUpdateEntity(orgInfoUpdateDto);
        orgInfoUpdateDto.setIsEnable(IsEnabledEnum.NO.getIndex());
        return orgInfoService.updateByUniqueNo(orgInfoUpdateDto);
    }

    /**
     * 根据条件分页查询合同
     * @param orgOrgContractQueryDto 机构合同查询
     * @param pageNum 当前页码
     * @param pageSize 每页显示条数
     * @param sortName 排序字段
     * @param sortOrder 排序方式
     * @return 机构合同分页列表
     */
    @RequestMapping(value = "/orgContractPage", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<OrgContractVo> orgContractPage(OrgContractQueryDto orgOrgContractQueryDto,
                                                @RequestParam(value = "page") int pageNum,
                                                @RequestParam(value = "rows") int pageSize,
                                                @RequestParam(value = "sidx") String sortName,
                                                @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return orgInfoService.contractPage(page, orgOrgContractQueryDto);
    }

    /**
     * 新增合同
     * @param orgContractCreateDto 机构合同新增
     * @return 机构合同
     */
    @RequestMapping(value = "/insertContract", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public OrgContractVo insertContract(OrgContractCreateDto orgContractCreateDto) {
        MfParamUtils.buildInsertEntity(orgContractCreateDto);
        return orgInfoService.insertContract(orgContractCreateDto);
    }

    /**
     * 根据唯一编号删除机构合同(逻辑删除)
     * @param orgContractDeleteDto 机构合同删除
     * @return 执行结果
     */
    @RequestMapping(value = "/deleteContract", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(OrgContractDeleteDto orgContractDeleteDto){
        MfParamUtils.buildUpdateEntity(orgContractDeleteDto);
        return orgInfoService.deleteContract(orgContractDeleteDto);
    }
}